﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.User.Security;
using System.IO;
using Xmobile.Users;
using System.Web.Caching;

namespace Portal.BO.Editoral.CommentManagement
{
    public class CommonHelper
    {        
        #region GetMenu
        /// <summary>
        /// Lấy ra menu news theo quyền của từng Role
        /// </summary>
        /// <param name="Prefix"></param>
        /// <returns></returns>
        public static DataTable GetDataPermission(String Prefix,int Type)
        {
            DataTable table = new DataTable();
            table.Columns.Add("PName",typeof(string));
            table.Columns.Add("Url", typeof(string));
            table.Columns.Add("Cpmode", typeof(string));
            table.Columns.Add("IsCount",typeof(bool));
            table.Columns.Add("Permission_ID", typeof(int));
            table.Columns.Add("ShortCutKey", typeof(string));

            int Role_ID = 0;

            switch (Prefix)
            { 
                case "pv":
                    Role_ID = RoleConst.PhongVien;
                    break;
                case "btv":
                    Role_ID = RoleConst.BienTapVien;
                    break;
                case "tkm":
                    Role_ID = RoleConst.ThuKyChuyenMuc;
                    break;
                case "tkts":
                    Role_ID = RoleConst.ThuKyToaSoan;
                    break;
            }
            //if (Role_ID == 0) { return table; }

            MainSecurity objSecu = new MainSecurity();
            //Permission permission = objSecu.GetPermission(HttpContext.Current.User.Identity.Name, Role_ID);
            Role role = objSecu.GetRole(HttpContext.Current.User.Identity.Name);

            DataTable permission = new DataTable();
            
            // Neu la user binh thuong thi check permission trong db
            if (Prefix == "")
            {
                permission = objSecu.GetPermissionAsTable(HttpContext.Current.User.Identity.Name);
            }
            else
            {
                if (role.isThuKyToaSoan || role.isBienTapVien || role.isThuKyChuyenMuc || role.isPhongVien)
                    permission = objSecu.GetPermissionAsTable(HttpContext.Current.User.Identity.Name, Role_ID);
                else
                    if (role.isQuanTriKenh || role.isPhuTrachKenh || role.isTongBienTap)
                    {
                        permission = objSecu.GetPermissionAsTable(HttpContext.Current.User.Identity.Name);
                    }
            }
            
                        
            DataTable tableXml;
            if (Type == 1)
            {
                tableXml = GetPermissionMenu("PermissionMenuNews.xml");
            }
            else if (Type == 2)
            {
                tableXml = GetPermissionMenu("PermissionMenuAlbum.xml");
            }
            else
            {
                tableXml = GetPermissionMenu("PermissionMenuReport.xml");
            }
            //DataView dv = tableXml.DefaultView;

            DataRow drInsert;
            
            foreach (DataRow dr in permission.Rows)
            {
                foreach (DataRow drx in tableXml.Rows)
                {
                    if (drx["Permission_ID"].ToString() == dr["Permission_ID"].ToString())
                    {
                        drInsert = table.NewRow();
                        drInsert["Permission_ID"] = drx["Permission_ID"].ToString();
                        drInsert["PName"] = drx["MenuName"].ToString();
                        drInsert["IsCount"] = drx["IsCount"].ToString();
                        drInsert["ShortCutKey"] = drx["ShortCutKey"] != null ? drx["ShortCutKey"].ToString() : "";

                        if ((drx["Role"].ToString().Equals("pv") && Prefix == "pv") || (drx["Role"].ToString().Equals("btv") && Prefix == "btv") || drx["Role"].ToString().Equals(""))
                        {
                            if (drx["IsPrefix"].ToString().Equals("True"))
                            {
                                drInsert["Url"] = drx["Url"].ToString() + Prefix + ".chn";
                                drInsert["Cpmode"] = drx["Cpmode"].ToString() + Prefix;
                            }
                            else
                            {
                                drInsert["Url"] = drx["Url"].ToString() + ".chn";
                                drInsert["Cpmode"] = drx["Cpmode"].ToString();
                            }
                            table.Rows.Add(drInsert);
                        }                        
                    }                    
                }
            }                        
            return table;
        }
        #endregion
        #region GetMenuAlbum
        /// <summary>
        /// Lấy ra meny album theo quyền của từng album
        /// </summary>
        /// <param name="Prefix"></param>
        /// <returns></returns>
        public static DataTable GetDataPermissionMenuAlbum(String Prefix)
        {
            DataTable table = new DataTable();
            table.Columns.Add("PName", typeof(string));
            table.Columns.Add("Url", typeof(string));
            table.Columns.Add("Cpmode", typeof(string));
            table.Columns.Add("IsCount", typeof(bool));

            int Role_ID = 0;

            switch (Prefix)
            {
                case "pv":
                    Role_ID = RoleConst.PhongVien;
                    break;
                case "btv":
                    Role_ID = RoleConst.BienTapVien;
                    break;
                case "tkm":
                    Role_ID = RoleConst.ThuKyChuyenMuc;
                    break;
                case "tkts":
                    Role_ID = RoleConst.ThuKyToaSoan;
                    break;
            }
            if (Role_ID == 0) { return table; }

            MainSecurity objSecu = new MainSecurity();
            Permission permission = objSecu.GetPermission(HttpContext.Current.User.Identity.Name, Role_ID);
            Role role = objSecu.GetRole(HttpContext.Current.User.Identity.Name);

            DataRow dr;
            if (permission.isBien_Tap_Tin_Anh)
            {
                //album_editwaitlist_
                dr = table.NewRow();
                dr["PName"] = "Danh sách album chờ biên tập";
                dr["Url"] = "~/office/album_editwaitlist_" + Prefix + ".chn";
                dr["Cpmode"] = "album_editwaitlist_" + Prefix;
                dr["IsCount"] = true;
                table.Rows.Add(dr);

                //album_editinglist_
                dr = table.NewRow();
                dr["PName"] = "Danh sách album nhận biên tập";
                dr["Url"] = "~/office/album_editinglist_" + Prefix + ".chn";
                dr["Cpmode"] = "album_editinglist_" + Prefix;
                dr["IsCount"] = true;
                table.Rows.Add(dr);
            }
            if (permission.isDuyet_Comment_Tin_Anh)
            { 
                
            }
            if (permission.isDuyet_Tin_Anh)
            {
                //album_approvalwaitlist_
                dr = table.NewRow();
                dr["PName"] = "Danh sách album chờ duyệt";
                dr["Url"] = "~/office/album_approvalwaitlist_" + Prefix + ".chn";
                dr["Cpmode"] = "album_approvalwaitlist_" + Prefix;
                dr["IsCount"] = true;
                table.Rows.Add(dr);

                //album_approvinglist_
                dr = table.NewRow();
                dr["PName"] = "Danh sách album nhận duyệt";
                dr["Url"] = "~/office/album_approvinglist_" + Prefix + ".chn";
                dr["Cpmode"] = "album_approvinglist_" + Prefix;
                dr["IsCount"] = true;
                table.Rows.Add(dr);
            }
            if (permission.isLuu_Tam_Tin_Anh)
            {
                //album_templist_
                dr = table.NewRow();
                dr["PName"] = "Danh sách album lưu tạm";
                dr["Url"] = "~/office/album_templist_" + Prefix + ".chn";
                dr["Cpmode"] = "album_templist_" + Prefix;
                dr["IsCount"] = true;
                table.Rows.Add(dr);
            }
            if (permission.isTao_Moi_Tin_Anh)
            {
                //album_add_
                dr = table.NewRow();
                dr["PName"] = "Album mới";
                dr["Url"] = "~/office/album_add_" + Prefix + ".chn";
                dr["Cpmode"] = "album_add_" + Prefix;
                dr["IsCount"] = false;
                table.Rows.Add(dr);
            }
            if (permission.isXem_DS_Tin_Anh_Da_Gui)
            {
                if (role.isPhongVien)
                {
                    //album_sendlist_
                    dr = table.NewRow();
                    dr["PName"] = "Danh sách album gửi chờ biên tập";
                    dr["Url"] = "~/office/album_sendlist_" + Prefix + ".chn"; ;
                    dr["Cpmode"] = "album_sendlist_" + Prefix;
                    dr["IsCount"] = true;
                    table.Rows.Add(dr);
                }
                if (role.isBienTapVien)
                {
                    //album_sendapprovallist
                    dr = table.NewRow();
                    dr["PName"] = "Danh sách album gửi chờ duyệt";
                    dr["Url"] = "~/office/album_sendapprovallist.chn";
                    dr["Cpmode"] = "album_sendapprovallist";
                    dr["IsCount"] = true;
                    table.Rows.Add(dr);
                }
            }
            if (permission.isXuat_Ban_Tin_Anh)
            {
                //album_publishedlist_
                dr = table.NewRow();
                dr["PName"] = "Danh sách album đã xuất bản";
                dr["Url"] = "~/office/album_publishedlist_" + Prefix + ".chn";
                dr["Cpmode"] = "album_publishedlist_" + Prefix;
                dr["IsCount"] = true;
                table.Rows.Add(dr);

                //album_removedlist_
                dr = table.NewRow();
                dr["PName"] = "Danh sách album đã gỡ bỏ";
                dr["Url"] = "~/office/album_removedlist_" + Prefix + ".chn";
                dr["Cpmode"] = "album_removedlist_" + Prefix;
                dr["IsCount"] = true;
                table.Rows.Add(dr);
            }

            return table;
        }
        #endregion
        #region GetPermissionMenu

        public static DataTable GetPermissionMenu(int Type)
        {
            DataTable table = new DataTable();
            switch (Type)
            { 
                case 1:
                    table = GetPermissionMenu("PermissionMenuNews.xml");
                    break;
                case 2:
                    table = GetPermissionMenu("PermissionMenuAlbum.xml");
                    break;
                case 3:
                    table = GetPermissionMenu("PermissionMenuReport.xml");
                    break;
            }
            return table;
        }

        private static DataTable GetPermissionMenu(String FileName)
        {
            string cacheName = "GetPermissionMenu" + FileName;
            DataTable dtbl = new DataTable();

            dtbl = HttpContext.Current.Cache.Get(cacheName) as DataTable;
            

            if (dtbl == null)
            {
                DataSet dset = new DataSet();
                String root = HttpContext.Current.Request.PhysicalApplicationPath.Replace(@"\", "/");
                FileStream fstr = new FileStream(root + "GUI/EditoralOffice/MainOffce/Menu/" + FileName, FileMode.Open, FileAccess.Read);
                dset.ReadXml(fstr);

                dtbl = dset.Tables[0];

                fstr.Close();
                dset.Dispose();

                //HttpContext.Current.Cache.Insert(cacheName, dataToCache, sqlDependency, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration);
                HttpContext.Current.Cache.Insert(cacheName, dtbl, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
            }

              return dtbl;

          }
        #endregion


        public static string ConvertVNDay(string EDay)
        {
            string Vday = "";
            switch (EDay)
            {
                case "Monday": Vday = "Thứ Hai"; break;
                case "Tuesday": Vday = "Thứ Ba"; break;
                case "Wednesday": Vday = "Thứ Tư"; break;
                case "Thursday": Vday = "Thứ Năm"; break;
                case "Friday": Vday = "Thứ Sáu"; break;
                case "Saturday": Vday = "Thứ Bẩy"; break;
                case "Sunday": Vday = "Chủ Nhật"; break;
            }
            return Vday;
        }
      }

    public class RolePrefix
    {
        public static String PHONGVIEN = "pv";
        public static String BIENTAPVIEN = "btv";
        public static String THUKYTOANSOAN = "tkts";
        public static String THUKYMUC = "tkm";
    }
}
